

import 'package:bt_tool/basic/bt_model.dart';
import 'package:flutter_demo/tools/json/json_main_controller.dart';


class JsonMainModel {

  String fileName = "";

  //完整的类文件字符串
  String jsonStr = "";

  //属性的字符串
  String parameterStr = "";

  //字典转属性的字符串,flutter使用
  String parseStr = "";

  //属性转字典的字符串,flutter使用
  String toMapStr = "";

  String ocClassDict = "";

  String getDisplayFileName(){
    return "${fileName}Model";
  }

  String getFullStr(ToolsJsonType type){
    String displayFileName = getDisplayFileName();
    if(type == ToolsJsonType.flutter){
      return "class $displayFileName extends BTModel{\n\n$parameterStr\n\n$displayFileName(super.oriJsonData);\n\n@override\nvoid parse(){\n$parseStr\n}\n\n@override\nMap<String,dynamic> convertToDict(){\nvar dict = super.convertToDict();\n$toMapStr\nreturn dict;\n}\n}";
    }

    if(type == ToolsJsonType.objectC){
      if(ocClassDict.isEmpty){
        return "@interface $displayFileName : BTModel\n\n$parameterStr\n\n@end\n\n\n\n@implementation $displayFileName \n@end\n\n";
      }
      return "@interface $displayFileName : BTModel\n\n$parameterStr\n\n@end\n\n\n\n@implementation $displayFileName\n\n- (void)initSelf{\n    [super initSelf];\n    self.classDict = @{\n        $ocClassDict\n    };\n}\n\n@end\n\n";
    }

    if(type == ToolsJsonType.swift){
      return "open class $displayFileName : ATBaseModel{\n$parameterStr\n\nopen override func parse(){\nlet jsonTool = ATJsonTool.create(with: oriDict)\n$parseStr\n}\n\n\nopen override func convertToDict() -> [String : Any] {\nvar dict = super.convertToDict()\n$toMapStr\nreturn dict\n}\n}";
    }

    if(type == ToolsJsonType.swiftObjectC){
      return "@objcMembers\n@objc open class $displayFileName : BTModel{\n$parameterStr\n}";
    }

    return "";
  }

}


class TestAddressModel extends BTModel {
  String city = "";
  String postalCode = "";
  TestAddressModel(super.oriJsonData);
  @override
  void parse(){
    city = jsonTool.getString("city");
    postalCode = jsonTool.getString("postalCode");
  }
  @override
  Map<String,dynamic> convertToDict(){
    var dict = super.convertToDict();
    dict["city"] = city;
    dict["postalCode"] = postalCode;
    return dict;
  }
}

class TestHobbiesModel extends BTModel {
  String name = "";
  String level = "";
  TestHobbiesModel(super.oriJsonData);
  @override
  void parse(){
    name = jsonTool.getString("name");
    level = jsonTool.getString("level");
  }
  @override
  Map<String,dynamic> convertToDict(){
    var dict = super.convertToDict();
    dict["name"] = name;
    dict["level"] = level;
    return dict;
  }
}


class TestModel extends BTModel{
  String name = "";
  int age = 0;
  bool isStudent = false;
  List<String> skills = [];
  TestAddressModel address = TestAddressModel(null);
  List<TestHobbiesModel> hobbies = [];
  List<double> testArray_1 = [];
  List<int> testArray_2 = [];
  List<dynamic> testArray_3 = [];
  dynamic testNull;
  TestModel(super.oriJsonData);
  @override
  void parse(){
    name = jsonTool.getString("name");
    age = jsonTool.getInt("age");
    isStudent = jsonTool.getBool("isStudent");
    skills = jsonTool.getStrList("skills");
    address = TestAddressModel(jsonTool.getMap("address"));
    for(var map in jsonTool.getMapList("hobbies")){
      TestHobbiesModel model = TestHobbiesModel(map);
      hobbies.add(model);
    }
    testArray_1 = jsonTool.getDoubleList("testArray_1");
    testArray_2 = jsonTool.getIntList("testArray_2");
    testArray_3 = jsonTool.getList("testArray_3");
    testNull = jsonTool.getDynamic("testNull");
  }
  @override
  Map<String,dynamic> convertToDict(){
    var dict = super.convertToDict();
    dict["name"] = name;
    dict["age"] = age;
    dict["isStudent"] = isStudent;
    dict["skills"] = skills;
    dict["address"] = address.convertToDict();
    dict["hobbies"] = convertModelListToDictList(hobbies);
    dict["testArray_1"] = testArray_1;
    dict["testArray_2"] = testArray_2;
    dict["testArray_3"] = testArray_3;
    dict["testNull"] = testNull;
    return dict;
  }
}